//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2013 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ITERATOR_CRANGE_H_
#define _BUOLA_ITERATOR_CRANGE_H_

#include <buola/buola.h>
#include <buola/iterator/traits.h>
#include <list>

namespace buola {

///\addtogroup iterators
///@{

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// iterator range

/// Holds a pair of iterators, representing the first and past-the-end items of a range and
/// offers a container-like interface to it.
///
/// It is used mostly as the return type from the \ref range set of functions.
///
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<typename tIt>
struct CRange : public std::pair<tIt,tIt>
{
    typedef typename std::iterator_traits<tIt>::value_type value_type;
    typedef tIt iterator;
    typedef tIt const_iterator;
    
    ///construct an invalid range
    CRange()=default;

    ///construct the range from a couple of iterators
    CRange(const tIt &pFirst,const tIt &pLast)
        :   std::pair<tIt,tIt>(pFirst,pLast)
    {}
    
    explicit CRange(const std::pair<tIt,tIt> &pPair)
        :   std::pair<tIt,tIt>(pPair)
    {}

    ///copy constructor
    CRange(const CRange&)=default;
    
    ///constructor from compatible range
    template<typename tRH>
    CRange(const CRange<tRH> &pRH)
        :   std::pair<tIt,tIt>(pRH.begin(),pRH.end())
    {}
    
    ///returns the first element in the iterator pair
    const tIt &begin() const            {   return this->first;                                             }
    ///returns the second element in the iterator pair
    const tIt &end() const              {   return this->second;                                            }

    ///returns `true` if the range is empty
    bool empty() const                  {   return this->first==this->second;   }
    ///returns the size of the range
    template<typename tRandom=QIteratorSupports<tIt,std::random_access_iterator_tag>,OEnableIf<tRandom> vDummy=0>
    std::size_t size() const            {   return this->second-this->first;    }
};

///@}

/*namespace buola*/ }

#endif
